package edu.wpi.cs509.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.wpi.cs509.common.exceptions.ServerUnreachable;
import edu.wpi.cs509.ui.intfc.PlayerStatsInterface;

/**
 * Panel that is used to display personal stats
 * 
 * @author jprocess
 *
 */
@SuppressWarnings("serial")
public class PersonalStatsPanel extends JPanel{
	A__MainStrategoClient client;
	JPanel mainPanel = new JPanel();
	JPanel statsPanel = new JPanel();
	JTextField nameField = new JTextField(8);
	
	public PersonalStatsPanel (A__MainStrategoClient client) {
		this.client = client;
		client.setStatsPanel(this);
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		//create and add query fields
		JPanel queryPanel = new JPanel();
		JLabel nameLabel = new JLabel();
		JButton getStatsButton = new JButton();
		
		//create label text and add action listeners
		nameLabel.setText("Player Name:");
		getStatsButton.setText("Submit");
		getStatsButton.addActionListener(new getStatsButtonAction());
		nameField.addActionListener(new getStatsButtonAction());
		
		//add object to queryPanel
		queryPanel.add(nameLabel);
		queryPanel.add(nameField);
		queryPanel.add(getStatsButton);
		
		//add panels to main panel
		add(queryPanel);
		add(statsPanel);
	}
	
	public void UpdateStats(PlayerStatsInterface myStats) {
		//clean up old panel
		statsPanel.setVisible(false);
		remove(statsPanel);
		statsPanel.removeAll();
		
		//create generic labels
		JLabel userLabel = new JLabel("Username: ");
		JLabel rankLabel = new JLabel("Rank: ");
		JLabel wonLabel = new JLabel("Games Won: ");
		JLabel lostLabel = new JLabel("Games Lost: ");
		JLabel ratioLabel = new JLabel("Win/Loss Ratio: ");
		
		//fill in stat data on labels
		JLabel userStatLabel = new JLabel(myStats.getUsername());
		JLabel rankStatLabel = new JLabel("" + myStats.getRank());
		JLabel wonStatLabel = new JLabel("" + myStats.getGamesWon());
		JLabel lostStatLabel = new JLabel("" + myStats.getGamesLost());
		
		//compute win/loss ratio and add to label
		JLabel ratioStatLabel = new JLabel();
		if (myStats.getGamesLost() == 0) {
			ratioStatLabel.setText("100.0");
		} else {
			ratioStatLabel.setText("" + ((float)myStats.getGamesWon())/((float)myStats.getGamesLost()));
		}
		
		//Position and add labels to panel
	    GridBagConstraints c = new GridBagConstraints(); // Holds constraints for GUI components.
	    GridBagLayout gridbag = new GridBagLayout();
	    statsPanel.setLayout(gridbag);
	    
	    c.gridx = 0;
	    c.gridy = 0;
	    gridbag.setConstraints(userLabel, c);
	    statsPanel.add(userLabel);
	    c.gridx = 100;
	    c.gridy = 0;
	    gridbag.setConstraints(userStatLabel, c);
	    statsPanel.add(userStatLabel);
	    
	    c.gridx = 0;
	    c.gridy = 1;
	    gridbag.setConstraints(rankLabel, c);
	    statsPanel.add(rankLabel);
	    c.gridx = 100;
	    c.gridy = 1;
	    gridbag.setConstraints(rankStatLabel, c);
	    statsPanel.add(rankStatLabel);
	    
	    c.gridx = 0;
	    c.gridy = 2;
	    gridbag.setConstraints(wonLabel, c);
	    statsPanel.add(wonLabel);
	    c.gridx = 100;
	    c.gridy = 2;
	    gridbag.setConstraints(wonStatLabel, c);
	    statsPanel.add(wonStatLabel);
	    
	    c.gridx = 0;
	    c.gridy = 3;
	    gridbag.setConstraints(lostLabel, c);
	    statsPanel.add(lostLabel);
	    c.gridx = 100;
	    c.gridy = 3;
	    gridbag.setConstraints(lostStatLabel, c);
	    statsPanel.add(lostStatLabel);
	    
	    c.gridx = 0;
	    c.gridy = 4;
	    gridbag.setConstraints(ratioLabel, c);
	    statsPanel.add(ratioLabel);
	    c.gridx = 100;
	    c.gridy = 4;
	    gridbag.setConstraints(ratioStatLabel, c);
	    statsPanel.add(ratioStatLabel);
		
		//redisplay/redraw panels
		statsPanel.setVisible(true);
		add(statsPanel);
		validate();
		repaint();
	}
	
	//action listener called when getStatsButton is pressed
	private class getStatsButtonAction implements ActionListener {
		getStatsButtonAction() {	
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				//make request to client logic for player stats
				client.getClientLogic().requestPersonalStats(nameField.getText());
			} catch (ServerUnreachable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
